﻿using System.Windows.Forms;
using System.Collections;
using Snarl;

namespace NativeWindowApplication
{

    // Summary description for WittySnarlMsgWnd.
    [System.Security.Permissions.PermissionSet(System.Security.Permissions.SecurityAction.Demand, Name = "FullTrust")]

    public class snarlMsgWnd : NativeWindow
    {
        CreateParams cp = new CreateParams();

        public int SNARL_GLOBAL_MESSAGE;
        ArrayList rememberedIds = new ArrayList();
        ArrayList rememberedUrls = new ArrayList();
        private string iconPath = System.IO.Path.GetDirectoryName(System.Windows.Forms.Application.ExecutablePath) + "\\Gmail_logo.jpg";
        

        public snarlMsgWnd()
        {
            // Create the actual window
            this.CreateHandle(cp);
            this.SNARL_GLOBAL_MESSAGE = Snarl.SnarlConnector.GetGlobalMsg();
            
        }

        public bool registerUrl(int msgId, string url) {
            try
            {
                rememberedUrls.Add(url);
                rememberedIds.Add(msgId);
                return true;
            }
            catch
            {
                return false;
            }
        }

        protected override void WndProc(ref Message m)
        {
            if (m.Msg == this.SNARL_GLOBAL_MESSAGE)
            {
                if ((int)m.WParam == Snarl.SnarlConnector.SNARL_LAUNCHED)
                {
                    Snarl.SnarlConnector.GetSnarlWindow(true);

                    SnarlConnector.RegisterConfig(this.Handle, "GMail Notifier", Snarl.WindowsMessage.WM_USER + 58, iconPath);
                    SnarlConnector.RegisterAlert("GMail Notifier", "New email");
                    SnarlConnector.RegisterAlert("GMail Notifier", "More than maximum number");
                }
            }

            if (m.Msg == (int)Snarl.WindowsMessage.WM_USER + 58)
            {
                if ((int)m.WParam == Snarl.SnarlConnector.SNARL_NOTIFICATION_ACK)
                {
                    int i = rememberedIds.BinarySearch(m.LParam.ToInt32());
                    if(i >= 0) {
                        try
                        {
                            System.Diagnostics.Process.Start(rememberedUrls[i].ToString());
                        }
                        catch
                        {
                            // not memorized correctly
                        }
                    }
                }
                else if ((int)m.WParam == Snarl.SnarlConnector.SNARL_NOTIFICATION_CLICKED)
                {
                    Snarl.SnarlConnector.SetTimeout(m.LParam.ToInt32(),0);
                }

                else if ((int)m.WParam == Snarl.SnarlConnector.SNARL_NOTIFICATION_TIMED_OUT)
                {
                    int i = rememberedIds.BinarySearch(m.LParam.ToInt32());
                    if (i >= 0)
                    {
                        try
                        {
                            rememberedUrls.RemoveAt(i);
                            rememberedIds.RemoveAt(i);
                        }
                        catch
                        {
                            // not memorized correctly
                        }
                    }
                }

            }

            base.WndProc(ref m);

        }

    }

}
